#pragma once
#include "libvcs.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>
#include <string>

#include <unordered_map>
#include <thread>
#include <mutex>

#include "class_custom.h"

static struct st_par par;


class cls_lib
{
public:
	type_subscribe_int subscribe_int = nullptr;
	type_subscribe_float subscribe_float = nullptr;
	type_subscribe_bool subscribe_bool = nullptr;
	type_log_send log_send = nullptr;
	type_error_send error_send = nullptr;
	type_run_server_trig run_server_trig = nullptr;

	cls_lib(std::string &libpath);
	~cls_lib();

	int RunTick(void);
	static int tick(void);
    void Init(std::shared_ptr<boost::circular_buffer<st_par>> &read_buf, std::shared_ptr<boost::circular_buffer<st_par>> &write_buf);

	inline void subscribe_int_init(const char *v_name, int &x);
	inline void subscribe_float_init(const char *v_name, float &x);
	inline void subscribe_bool_init(const char *v_name, bool &x);

	const std::unordered_map<std::string, int *> GetIntMap();
	const std::unordered_map<std::string, float *> GetFloatMap();
	const std::unordered_map<std::string, bool *> GetBoolMap();

private:
	void *handle;

	void *dl_adrress_get(void *_handle, const char *_name);

	std::mutex g_mutex;

	std::unordered_map<std::string, int *> um_int_;
	std::unordered_map<std::string, float *> um_float_;
	std::unordered_map<std::string, bool *> um_bool_;

	static std::shared_ptr<boost::circular_buffer<st_par>> read_buffer_;
	static std::shared_ptr<boost::circular_buffer<st_par>> write_buffer_;

	static void ReadMsgChange();
	static void WriteMsgChange();
};